<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8">
<title>全部分类</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport"
	content="width=device-width, initial-scale=1, maximum-scale=1">
<link rel="stylesheet" th:href="@{/layui/css/layui.css}" media="all">
<script th:src="@{/layui/layui.js}"></script>
<script type="text/javascript">

layui.use(['form', 'jquery'] ,
function() {
    var form = layui.form;
    var $ = layui.jquery;
    var layer = layui.layer;
    //自定义验证规则  
    form.verify({
         password: [/^[0-9a-zA-Z]{6,12}$/, '密码只能是6-12位的数字或字母组合！'],
         password2 : function(value, item){
            if(!new RegExp($("#password").val()).test(value)){
                return "两次输入密码不一致，请重新输入！";
            }
          },
          token :function(value, item){
	            if(!new RegExp(/^[0-9a-zA-Z]{31,64}$/).test(value)){
	                return "请输入31位以上的token";
	            }
          },userName: [/^[a-z0-9_-]{3,16}$/  ,'用户名必须是3~16位数字、字母、下划线。']
          	
          
        
    });
    var userName = '[[${session.currentUser.userName}]]';
   	$("#username").val(userName); //显示当前登录的用户名
    form.on('submit(modifyPassword)', 
    function(data) {
    $.ajax({
            async: false,
            url: '../admin/blogger/modtifyPassword',
            data: {
                'userName': $("#username").val(), //提交用户名
                'password': $("#password").val(), 
                'token':    $("#token").val(),
            },
            type: 'post',
            success: function(result) {
          		if(typeof result === 'string') {
					 result = JSON.parse(result)
				}
                if (result.success==1) {
                    layer.alert('修改成功，下次登录生效！', {
                        title: '系统提示',
                        icon: 6,
                        closeBtn: 0,
                    },
                    function(index) {
                        layer.close(index); //关闭
                        $("#password").val("");
                        $("#password2").val("");
                        $("#token").val("");
                    });
                } else if(result.success==-1) {
                    layer.alert('token令牌错误！', {
                        title: '系统提示',
                        icon: 5,
                        closeBtn: 0,
                    },
                    function(index) {
                        layer.close(index); //关闭
                        $("#password2").val("");
                        $("#password").val("");
                    	$("#token").val("");
                    });
                } else {
                	 layer.alert('未知错误，修改失败！', {
                        title: '系统提示',
                        icon: 5,
                        closeBtn: 0,
                    },
                    function(index) {
                        layer.close(index); //关闭
                        	$("#password").val("");
                        	$("#password2").val("");
                        	$("#token").val("");
                    });
                }
            } // end success
        }); 
        return false;
    }); //end form.on
    //注册button的事件
     var $ = layui.$,
   		 active = {
        /**
		 *定义函数绑定到按钮的data-type上 delBlogType
		 */
        close: function() {
       		 //清空密码框
        	$("#password").val("");
        	$("#password2").val("");
       		 location.reload();
        	
        },
      }
        $('.layui-form-item .layui-btn').on('click',
    function() {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });
    
});
</script>
</head>
<body>
	<form class="layui-form layui-form-pane " action="" id="modifyPassword"
		style="margin-left: 450px;margin-top: 75px;">
		<div class="layui-form-item">
			<label class="layui-form-label">用户名：</label>
			<div class="layui-input-inline">
				<input type="text" name="username" id="username"
					lay-verify="userName" placeholder="Username" autocomplete="off"
					class="layui-input">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">新密码：</label>
			<div class="layui-input-inline">
				<input type="password" name="password" placeholder="Password"
					autocomplete="off" class="layui-input" id="password"
					lay-verify="password">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">确认密码：</label>
			<div class="layui-input-inline">
				<input type="password" name="password2" placeholder="Password"
					autocomplete="off" class="layui-input" id="password2"
					lay-verify="password2">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">Token：</label>
			<div class="layui-input-inline">
				<input type="password" name="token" placeholder="Token"
					autocomplete="off" class="layui-input" id="token"
					lay-verify="token">
			</div>
		</div>
		<div class="layui-form-item">
			<button class="layui-btn" lay-submit="" lay-filter="modifyPassword"
				style="margin-left: 50px;">确定</button>
			<button class="layui-btn" style="margin-left: 50px;"
				data-type="close">取消</button>

		</div>
	</form>
</body>
</html>